//
//  RecAllStoryViewController.m
//  TravelEveryDay
//
//  Created by dlios on 15-10-16.
//  Copyright (c) 2015年 com.lanou3g. All rights reserved.
//

#import "RecAllStoryViewController.h"
#import "RecStoryCollectionViewCell.h"
#import "RecStoryModel.h"
#import "RecStoryUserModel.h"
#import "RecStoryDetailsViewController.h"
@interface RecAllStoryViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation RecAllStoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitle:@"每日精选故事"];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    _storyArr = [[NSMutableArray alloc] init];
    _storyUserArr = [[NSMutableArray alloc] init];
    [self setUpCollectionView];
    [self addHeader];
    [self addFooter];
}

#pragma mark - collection创建
-(void)setUpCollectionView
{
    //flowLayout设置
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(160 * WIDTH, 250 * HEIGHT);
    flow.minimumInteritemSpacing = 10;
    flow.minimumLineSpacing = 10;
    [flow setSectionInset:UIEdgeInsetsMake(20 * HEIGHT, 20 * WIDTH, 20 * HEIGHT, 20 * WIDTH)];
    
    //collectionView创建
    _collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 114 * HEIGHT) collectionViewLayout:flow];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
    [_collection registerClass:[RecStoryCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    [self.view addSubview:_collection];
}
#pragma mark - collection Delegate & DataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _storyArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RecStoryModel *model = [_storyArr objectAtIndex:indexPath.row];
    RecStoryUserModel *userModel = [_storyUserArr objectAtIndex:indexPath.row];
    
    RecStoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    [cell getData:model];
    [cell getUserData:userModel];
    return cell;

}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    RecStoryModel *model = [_storyArr objectAtIndex:indexPath.row];
    RecStoryUserModel *userModel = [_storyUserArr objectAtIndex:indexPath.row];
    RecStoryDetailsViewController *story = [[RecStoryDetailsViewController alloc] init];
    [self.navigationController pushViewController:story animated:YES];
    story.userModel = userModel;
    story.myId = model.spot_id;
}
#pragma mark - 数据获取
-(void)getData:(NSInteger)number upOrDown:(BOOL)upOrDown
{
    [Refresh getdataWithString:[NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=%ld",number] block:^(id block) {
        if (upOrDown == NO) {
            [_storyArr removeAllObjects];
            [_storyUserArr removeAllObjects];
        }
         for (NSDictionary *dic in [[block objectForKey:@"data"] objectForKey:@"hot_spot_list"]) {
             
                 RecStoryModel *model = [RecStoryModel modelWithDictionary:dic];
                 [_storyArr addObject:model];
                
                 RecStoryUserModel *userModel = [RecStoryUserModel modelWithDictionary:[dic objectForKey:@"user"]];
                 [_storyUserArr addObject:userModel];
             
         }
        [_collection reloadData];
        [_collection.header endRefreshing];
        [_collection.footer endRefreshing];
        
    }];
}
#pragma mark - 刷新加载
-(void)addHeader
{
    __block RecAllStoryViewController *rec = self;
    [_collection addLegendHeaderWithRefreshingBlock:^{
        rec.upOrDown = NO;
        [rec getData:0 upOrDown:rec.upOrDown];
    }];
    [_collection.header beginRefreshing];
}
-(void)addFooter
{
    __block RecAllStoryViewController *rec = self;
    [_collection addLegendFooterWithRefreshingBlock:^{
        rec.page = rec.page + 12;
        rec.upOrDown = YES;
        [rec getData:rec.page upOrDown:rec.upOrDown];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
